package com.dpj.servlet;

import com.dpj.pojo.Brand;
import com.dpj.service.BrandService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addBrandServlet")
public class AddBrandServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
         Integer id = Integer.valueOf(req.getParameter("id"));
        String brandName = req.getParameter("brandName");
        String companyName = req.getParameter("companyName");
        Integer ordered = Integer.valueOf(req.getParameter("ordered"));
        Integer status = Integer.valueOf(req.getParameter("status"));
        String description = req.getParameter("description");
        BrandService service=new BrandService();
        if(id==null){
            Brand brand=new Brand(null,brandName,companyName,ordered,description,status);

            int add = service.add(brand);
            if(add>0){
                resp.sendRedirect("/w9/brandServlet");

            }else {
                resp.getWriter().write("增加失败");
            }
        }else {
            Brand brand=new Brand(id,brandName,companyName,ordered,description,status);
            int i = service.updateByID(brand);
            if(i>0){
                resp.sendRedirect("/w9/brandServlet");
            }else {
                resp.getWriter().write("更新失败了");
            }

        }

    }
}
